package StudyCode.summary.myfile;

import java.util.Arrays;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ConvertsIntegerToByteLength4 {
	public static void main(String[] argv) throws Exception {
		int a = 2;
		byte[] buf = new byte[]{0, 0, 0, 0};
		System.out.println(a + "转为长度为4的字节数组:");
		integerToByteArray(a, buf);
		System.out.println(Arrays.toString(buf));
	}

	private static void integerToByteArray(int a, byte[] buf) {
		buf[0] = (byte) ((a >> 24) & 0xFF);
		buf[1] = (byte) ((a >> 16) & 0xFF);
		buf[2] = (byte) ((a >> 8) & 0xFF);
		buf[3] = (byte) (a & 0xFF);
	}
}
